package praca.framework;

import java.io.IOException;

import praca.interfejsy.FileIO;
import praca.interfejsy.Game;
import praca.interfejsy.SoundManager;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.SoundPool;

public class SoundManagerGL implements SoundManager{

	SoundPool soundPool;
	Game game;
	FileIO io;
	AssetFileDescriptor descriptor;
	AssetManager manager;
	int soundId;
	
	public SoundManagerGL(Game game){
		soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
		this.game = game;
		io = game.getFileIO();
		manager = game.getAssetManager();
		
	}
	
	@Override
	public void play(int soundId) {
		if(game.isSoundEnabled())
		soundPool.play(soundId, 1.0f, 1.0f, 0, 0, 1);
		
	}

	@Override
	public void dispose(int soundId) {
		soundPool.unload(soundId);
		
	}
	
	@Override
	public int loadSound(String filename){
		try {
			descriptor = manager.openFd(filename);
			soundId = soundPool.load(descriptor, 1);
		} catch (IOException e) {
			soundId = -1;
		}

		return soundId;
	}
	
	

}
